//
//  UIView+TTotheAdd.m
//  TaTogether
//
//  Created by 九月 on 8/20/24.
//

#import "UIView+TTotheAdd.h"

#import <objc/runtime.h>


@implementation UIView (TTotheAdd)

- (UIImage *)ttotheImageFromView {
    UIGraphicsBeginImageContext(self.frame.size);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
    UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)tatogetPushControllerWithName:(NSString *)name {
    Class tatogetClass = NSClassFromString(name);
    UIViewController *tatogetTargetVC = [tatogetClass new];
    [self.viewController.navigationController pushViewController:tatogetTargetVC animated:YES];
}

- (void)ttothe_cornerRadius:(CGFloat)cunguRadius cunhuaRectCorners:(UIRectCorner)habibiCorners {
    // 创建一个与视图大小相同的圆角路径
    UIBezierPath *mirchiPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                               byRoundingCorners:habibiCorners
                                                     cornerRadii:CGSizeMake(cunguRadius, cunguRadius)];
    
    // 创建一个形状层作为视图的遮罩
    CAShapeLayer *heiyouMaskLayer = [[CAShapeLayer alloc] init];
    heiyouMaskLayer.frame = self.bounds;
    heiyouMaskLayer.path = mirchiPath.CGPath;
    
    // 将形状层设置为视图的遮罩
    self.layer.mask = heiyouMaskLayer;
}

UIEdgeInsets kYOUFanSafaInsets(void) {
    return [UIApplication sharedApplication].windows.firstObject.safeAreaInsets;
}

CGFloat kYOUFanNavHeight(void) {
    return kYOUFanSafaInsets().top + 44;
}


- (void)setTtotheCornerRadius:(CGFloat)ttotheCornerRadius {
    // 设置关联对象，存储 ttotheCornerRadius
    objc_setAssociatedObject(self, @selector(ttotheCornerRadius), @(ttotheCornerRadius), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    // 设置圆角
    self.layer.cornerRadius = ttotheCornerRadius;
    self.layer.masksToBounds = YES;
    
    
}

- (CGFloat)ttotheCornerRadius {
    // 获取关联对象的值
    NSNumber *cornerRadius = objc_getAssociatedObject(self, @selector(ttotheCornerRadius));
    return cornerRadius ? [cornerRadius floatValue] : 0.0;
}

- (void)setTtotheScaleAspectFill:(BOOL)ttotheScaleAspectFill {
    // 设置关联对象，存储 ttotheScaleAspectFill 的值
    objc_setAssociatedObject(self, @selector(ttotheScaleAspectFill), @(ttotheScaleAspectFill), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if (ttotheScaleAspectFill) {
        // 设置 contentMode 和 clipsToBounds
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;
    }
}

- (BOOL)ttotheScaleAspectFill {
    // 获取关联对象的值
    NSNumber *scaleAspectFill = objc_getAssociatedObject(self, @selector(ttotheScaleAspectFill));
    return scaleAspectFill ? [scaleAspectFill boolValue] : NO;
}

- (UIViewController *)viewController {
    for (UIView *view = self; view; view = view.superview) {
        UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


- (CGFloat)left {
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)top {
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}


@end

